var maxSubArray = function(nums) {
    let m,sum=nums[0],max=nums[0];
    for(m=1;m<nums.length;m++){
        sum=Math.max(sum+nums[m],nums[m]);
        if(sum>max){
            max=sum;
        }
    }
    return max;
};

let n=[-2,1,-3,4,-1,2,1,-5,4];
console.log(maxSubArray(n));